import requests
import urllib3
from dataconfig.file_config import FilePath
from logs.user_log import UserLog
from util.operation_yaml import OperationYaml
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)


class OperationCookie:

	def __init__(self):
		self.path = FilePath()
		self.log = UserLog().get_log()
		self.op_yaml = OperationYaml()
		self.cookie__ = 'cookie'


	def get_cookie(self,res):
		# 获取cookie的jar文件
		cookie_jar = res.cookies
		self.log.debug('%s' %res)
		self.log.debug('获取cookie的jar文件, %s' % cookie_jar)
		# 转换cookie为json
		# self.log.debug('转换cookie为json')
		cookie_json = requests.utils.dict_from_cookiejar(cookie_jar)
		self.log.debug('转换cookie为json, %s' % cookie_json)
		return cookie_json

	# 根据关键字获取cookie123
	def get_data(self):
		data = self.op_yaml.read_data()[self.cookie__]
		self.log.debug('获取cookie, %s；cookie格式为 %s' % (data, type(data)))
		return data

	def updata_cookie(self, res):
		# 转换cookie为json
		self.log.debug('准备更新cookies')
		cookie_json = self.get_cookie(res)
		all_data  = self.op_yaml.read_data(); updata_cookie = all_data
		updata_cookie['cookie'] = cookie_json
		self.log.debug('更新cookie成功，%s' % cookie_json)
		self.op_yaml.write_data(updata_cookie)
		# self.op_json.write_data(self.path.cookie(),cookie_json)



if __name__ == '__main__':
	# res = json.dumps(requests.post(url,data).json())
	# op_Cookie = OperationCookie(res)
	# op_Cookie.write_cookie()
	a01 = {'__outh': '2'}
	# print(OperationCookie().get_data())
	pass

